home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #3 / Amiga Plus CD - 2002 - No. 03.iso / AmigaPlus / Tools / Development / renderlib40 / src / rnd_conversion.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-12-20  |  9.7 KB  |  381 lines

  1.  
  2. #include "lib_init.h"
  3. #include "lib_debug.h"
  4. #include <render/render.h>
  5. #include <proto/utility.h>
  6. #include <proto/exec.h>
  7. #include <graphics/gfx.h>
  8.  
  9. /************************************************************************** 
  10. **
  11. **    remaparray
  12. */
  13.  
  14. LIBAPI void RemapArrayA(UBYTE *src, UWORD width, UWORD height, 
  15.     UBYTE *dst, UBYTE *pentab, struct TagItem *tags)
  16. {
  17.     if (src && width && height && dst && pentab)
  18.     {
  19.         LONG tsw = GetTagData(RND_SourceWidth, width, tags);
  20.         LONG tdw = GetTagData(RND_DestWidth, width, tags);
  21.         LONG y, x;
  22.         
  23.         for (y = 0; y < height; ++y)
  24.         {
  25.             for (x = 0; x < width; ++x)
  26.             {
  27.                 *dst++ = pentab[*src++];
  28.             }
  29.             src += tsw - width;
  30.             dst += tdw - width;
  31.         }
  32.     }
  33. }
  34.  
  35. /************************************************************************** 
  36. **
  37. **    createpentable
  38. */
  39.  
  40. LIBAPI void CreatePenTableA(UBYTE *src, RNDPAL *srcpal, UWORD width, UWORD height, 
  41.     RNDPAL *dstpal, UBYTE *pentab, struct TagItem *tags)
  42. {
  43.     if (src && srcpal && width && height && dstpal && pentab)
  44.     {
  45.         LONG y, x;
  46.         LONG i;
  47.         WORD table[256];
  48.         LONG tsw = GetTagData(RND_SourceWidth, width, tags);
  49.         UBYTE *secondary = (UBYTE *) GetTagData(RND_PenTable, NULL, tags);
  50.  
  51.         memfill32((ULONG *) table, 256 * sizeof(WORD), 0xffffffff);
  52.         
  53.         for (y = 0; y < height; ++y)
  54.         {
  55.             for (x = 0; x < width; ++x)
  56.             {
  57.                 i = *src++;
  58.                 table[i] = P2Lookup(dstpal, srcpal->table[i]);
  59.             }
  60.             src += tsw - width;
  61.         }
  62.         
  63.         if (secondary)
  64.         {
  65.             for (i = 0; i < 256; ++i)
  66.             {
  67.                 *pentab++ = secondary[table[i]];
  68.             }
  69.         }
  70.         else
  71.         {
  72.             for (i = 0; i < 256; ++i)
  73.             {
  74.                 *pentab++ = table[i];
  75.             }
  76.         }
  77.     }
  78. }
  79.  
  80. /************************************************************************** 
  81. **
  82. **    chunky2rgb
  83. */
  84.  
  85. LIBAPI ULONG Chunky2RGBA(UBYTE *src, UWORD width, UWORD height, 
  86.     ULONG *dst, RNDPAL *pal, struct TagItem *tags)
  87. {
  88.     ULONG result = CONV_NO_DATA;
  89.     if (src && width && height && dst && pal)
  90.     {
  91.         struct RND_ProgressMessage progmsg;
  92.         struct RND_LineMessage linemsg;
  93.  
  94.         struct Hook *proghook = (struct Hook *) GetTagData(RND_ProgressHook, NULL, tags);
  95.         struct Hook *linehook = (struct Hook *) GetTagData(RND_LineHook, NULL, tags);
  96.         LONG tsw = GetTagData(RND_SourceWidth, width, tags);
  97.         LONG tdw = GetTagData(RND_DestWidth, width, tags);
  98.         ULONG colormode = GetTagData(RND_ColorMode, COLORMODE_CLUT, tags);
  99.         LONG left = GetTagData(RND_LeftEdge, 0, tags);
  100.         UBYTE *pentab = (UBYTE *) GetTagData(RND_PenTable, NULL, tags);
  101.         LONG y, x;
  102.  
  103.         progmsg.RND_PMsg_type = PMSGTYPE_LINES_CONVERTED;
  104.         progmsg.RND_PMsg_total = height;
  105.  
  106.         result = CONV_CALLBACK_ABORTED;
  107.         
  108.         for (y = 0; y < height; ++y)
  109.         {
  110.             if (linehook)
  111.             {
  112.                 linemsg.RND_LMsg_type = LMSGTYPE_LINE_FETCH;
  113.                 linemsg.RND_LMsg_row = y;
  114.                 if (!CallHookPkt(linehook, src, &linemsg)) goto abort;
  115.             }        
  116.         
  117.             switch (colormode)
  118.             {
  119.                 case COLORMODE_HAM6:
  120.                 {
  121.                     UBYTE p = 0, nib;
  122.                     ULONG col;
  123.                     if (pentab) p = pentab[0];
  124.                     col = pal->table[p] & 0xf0f0f0;
  125.                     col |= col >> 4;
  126.                     for (x = 0; x < width + left; ++x)
  127.                     {
  128.                         p = *src++;
  129.                         if (pentab) p = pentab[p];
  130.                         nib = p & 15;
  131.                         switch ((p >> 4) & 3)
  132.                         {
  133.                             case 0:
  134.                                 col = pal->table[nib] & 0xf0f0f0;
  135.                                 col |= col >> 4;
  136.                                 break;
  137.                             case 2:
  138.                                 col &= 0x00ffff;
  139.                                 col |= nib << 20;
  140.                                 col |= nib << 16;
  141.                                 break;
  142.                             case 3:
  143.                                 col &= 0xff00ff;
  144.                                 col |= nib << 12;
  145.                                 col |= nib << 8;
  146.                                 break;
  147.                             case 1:
  148.                                 col &= 0xffff00;
  149.                                 col |= nib << 4;
  150.                                 col |= nib;
  151.                                 break;
  152.                         }
  153.                         if (x >= left) *dst++ = col;
  154.                     }
  155.                     break;
  156.                 }
  157.                 
  158.                 case COLORMODE_HAM8:
  159.                 {
  160.                     UBYTE p = 0, nib;
  161.                     ULONG col;
  162.                     if (pentab) p = pentab[0];
  163.                     col = pal->table[p];
  164.                     for (x = 0; x < width + left; ++x)
  165.                     {
  166.                         p = *src++;
  167.                         if (pentab) p = pentab[p];
  168.                         nib = p & 63;
  169.                         switch ((p >> 6) & 3)
  170.                         {
  171.                             case 0:
  172.                                 col = pal->table[nib];
  173.                                 break;
  174.                             case 2:
  175.                                 col &= 0x03ffff;
  176.                                 col |= nib << 18;
  177.                                 break;
  178.                             case 3:
  179.                                 col &= 0xff03ff;
  180.                                 col |= nib << 10;
  181.                                 break;
  182.                             case 1:
  183.                                 col &= 0xffff03;
  184.                                 col |= nib << 2;
  185.                                 break;
  186.                         }
  187.                         if (x >= left) *dst++ = col;
  188.                     }
  189.                     break;
  190.                 }
  191.     
  192.                 default:
  193.                 case COLORMODE_CLUT:
  194.                     src += left;
  195.                     if (pentab)
  196.                     {
  197.                         for (x = 0; x < width; ++x)
  198.                         {
  199.                             *dst++ = pal->table[pentab[*src++]];
  200.                         }
  201.                     }
  202.                     else
  203.                     {
  204.                         for (x = 0; x < width; ++x)
  205.                         {
  206.                             *dst++ = pal->table[*src++];
  207.                         }
  208.                     }
  209.                     break;
  210.             }
  211.  
  212.             if (linehook)
  213.             {
  214.                 linemsg.RND_LMsg_type = LMSGTYPE_LINE_RENDERED;
  215.                 linemsg.RND_LMsg_row = y;
  216.                 if (!CallHookPkt(linehook, dst - width, &linemsg)) goto abort;
  217.             }        
  218.  
  219.             if (proghook)
  220.             {
  221.                 progmsg.RND_PMsg_count = y;
  222.                 if (!CallHookPkt(proghook, NULL, &progmsg)) goto abort;
  223.             }
  224.  
  225.             src += tsw - width;
  226.             dst += tdw - width;
  227.         }
  228.  
  229.         result = CONV_SUCCESS;
  230.     }
  231.  
  232. abort:        
  233.     return result;
  234. }
  235.  
  236. /************************************************************************** 
  237. **
  238. **    planar2chunky
  239. */
  240.  
  241. LIBAPI void Planar2ChunkyA(UWORD **planetab, UWORD bytewidth, UWORD rows, UWORD depth, 
  242.     UWORD bytesperrow, UBYTE *dst, struct TagItem *tags)
  243. {
  244.     LONG y, w, x, offs;
  245.     LONG destwidth = GetTagData(RND_DestWidth, bytewidth * 8, tags);
  246.     UWORD mask;
  247.     UBYTE p;
  248.     
  249.     if (!planetab || !bytewidth || !rows || !depth || !dst) return;
  250.     
  251.     for (y = 0; y < rows; ++y)
  252.     {
  253.         for (w = 0; w < bytewidth/2; ++w)
  254.         {
  255.             offs = y * (bytesperrow>>1) + w;
  256.             mask = 0x8000;
  257.             for (x = 0; x < 16; ++x)
  258.             {
  259.                 p = 0;
  260.                 switch (depth)
  261.                 {
  262.                     default:
  263.                     case 8:          if (*(planetab[7] + offs) & mask) p |= 1;
  264.                     case 7: p <<= 1; if (*(planetab[6] + offs) & mask) p |= 1;
  265.                     case 6: p <<= 1; if (*(planetab[5] + offs) & mask) p |= 1;
  266.                     case 5: p <<= 1; if (*(planetab[4] + offs) & mask) p |= 1;
  267.                     case 4: p <<= 1; if (*(planetab[3] + offs) & mask) p |= 1;
  268.                     case 3: p <<= 1; if (*(planetab[2] + offs) & mask) p |= 1;
  269.                     case 2: p <<= 1; if (*(planetab[1] + offs) & mask) p |= 1;
  270.                     case 1: p <<= 1; if (*(planetab[0] + offs) & mask) p |= 1;
  271.                 }
  272.  
  273.                 dst[w*16 + x] = p;
  274.                 mask >>= 1;
  275.             }
  276.         }
  277.         dst += destwidth;
  278.     }
  279. }
  280.  
  281. /************************************************************************** 
  282. **
  283. **    chunky2bitmap
  284. */
  285.  
  286. LIBAPI void Chunky2BitMapA(UBYTE *src, UWORD sx, UWORD sy, UWORD width, UWORD height, 
  287.     struct BitMap *bm, UWORD dx, UWORD dy, struct TagItem *tags)
  288. {
  289.     LONG tsw;
  290.     UBYTE *pentab;
  291.     LONG y, x;
  292.     UWORD words[8];
  293.     if (!src || !bm || !width || !height) return;
  294.     tsw = GetTagData(RND_SourceWidth, width, tags);
  295.     pentab = (UBYTE *) GetTagData(RND_PenTable, NULL, tags);
  296.     
  297.     src += sx + sy * tsw;
  298.     
  299.     for (y = dy; y < dy + height; ++y)
  300.     {
  301.         for (x = dx; x < dx + width; ++x)
  302.         {
  303.             UBYTE p = *src++;
  304.             if (pentab) p = pentab[p];
  305.             switch (bm->Depth)
  306.             {
  307.                 default:
  308.                 case 8: words[7] <<= 1; words[7] |= (p & 128) >> 7;
  309.                 case 7: words[6] <<= 1; words[6] |= (p & 64) >> 6;
  310.                 case 6: words[5] <<= 1; words[5] |= (p & 32) >> 5;
  311.                 case 5: words[4] <<= 1; words[4] |= (p & 16) >> 4;
  312.                 case 4: words[3] <<= 1; words[3] |= (p & 8) >> 3;
  313.                 case 3: words[2] <<= 1; words[2] |= (p & 4) >> 2;
  314.                 case 2: words[1] <<= 1; words[1] |= (p & 2) >> 1;
  315.                 case 1: words[0] <<= 1; words[0] |= (p & 1);
  316.             }
  317.             
  318.             if (((x + 1) & 15) == 0)
  319.             {
  320.                 LONG offs = (y * bm->BytesPerRow + (x >> 3)) >> 1;
  321.                 
  322.                 if (x - dx < 15)    /* first word not at 16bit boundary, must be merged */
  323.                 {
  324.                     UWORD mask = 0xffff << (x - dx + 1);
  325.                 
  326.                     switch (bm->Depth)
  327.                     {
  328.                         default:
  329.                         case 8:    words[7] = (words[7] & ~mask) | (((UWORD *) bm->Planes[7])[offs] & mask);
  330.                         case 7:    words[6] = (words[6] & ~mask) | (((UWORD *) bm->Planes[6])[offs] & mask);
  331.                         case 6:    words[5] = (words[5] & ~mask) | (((UWORD *) bm->Planes[5])[offs] & mask);
  332.                         case 5:    words[4] = (words[4] & ~mask) | (((UWORD *) bm->Planes[4])[offs] & mask);
  333.                         case 4:    words[3] = (words[3] & ~mask) | (((UWORD *) bm->Planes[3])[offs] & mask);
  334.                         case 3:    words[2] = (words[2] & ~mask) | (((UWORD *) bm->Planes[2])[offs] & mask);
  335.                         case 2:    words[1] = (words[1] & ~mask) | (((UWORD *) bm->Planes[1])[offs] & mask);
  336.                         case 1:    words[0] = (words[0] & ~mask) | (((UWORD *) bm->Planes[0])[offs] & mask);
  337.                     }
  338.                 }
  339.                 
  340.                 switch (bm->Depth)
  341.                 {
  342.                     default:
  343.                     case 8:    ((UWORD *) bm->Planes[7])[offs] = words[7];
  344.                     case 7:    ((UWORD *) bm->Planes[6])[offs] = words[6];
  345.                     case 6:    ((UWORD *) bm->Planes[5])[offs] = words[5];
  346.                     case 5:    ((UWORD *) bm->Planes[4])[offs] = words[4];
  347.                     case 4:    ((UWORD *) bm->Planes[3])[offs] = words[3];
  348.                     case 3:    ((UWORD *) bm->Planes[2])[offs] = words[2];
  349.                     case 2:    ((UWORD *) bm->Planes[1])[offs] = words[1];
  350.                     case 1:    ((UWORD *) bm->Planes[0])[offs] = words[0];
  351.                 }
  352.             }
  353.         }
  354.         
  355.         if (x & 15)        /* remainder at the right, must be merged into planes */
  356.         {
  357.             LONG offs = (y * bm->BytesPerRow + (x >> 3)) >> 1;
  358.             UWORD mask;
  359.             mask = 0xffff >> x;
  360.             x = 16 - (x & 15);
  361.  
  362.             switch (bm->Depth)
  363.             {
  364.                 default:
  365.                 case 8:    ((UWORD *) bm->Planes[7])[offs] = ((UWORD *) bm->Planes[7])[offs] & mask | (words[7] << x);
  366.                 case 7:    ((UWORD *) bm->Planes[6])[offs] = ((UWORD *) bm->Planes[6])[offs] & mask | (words[6] << x);
  367.                 case 6:    ((UWORD *) bm->Planes[5])[offs] = ((UWORD *) bm->Planes[5])[offs] & mask | (words[5] << x);
  368.                 case 5:    ((UWORD *) bm->Planes[4])[offs] = ((UWORD *) bm->Planes[4])[offs] & mask | (words[4] << x);
  369.                 case 4:    ((UWORD *) bm->Planes[3])[offs] = ((UWORD *) bm->Planes[3])[offs] & mask | (words[3] << x);
  370.                 case 3:    ((UWORD *) bm->Planes[2])[offs] = ((UWORD *) bm->Planes[2])[offs] & mask | (words[2] << x);
  371.                 case 2:    ((UWORD *) bm->Planes[1])[offs] = ((UWORD *) bm->Planes[1])[offs] & mask | (words[1] << x);
  372.                 case 1:    ((UWORD *) bm->Planes[0])[offs] = ((UWORD *) bm->Planes[0])[offs] & mask | (words[0] << x);
  373.             }
  374.         }
  375.         
  376.  
  377.         src += tsw - width;
  378.     }
  379. }
  380.  
  381.